<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:">
      <el-input v-model="userInfo.password" type="password" style="width:300px" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="updateUserDetailById">更新</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getUserDetailById } from "@/api/user";
import { saveUserDetailById } from "@/api/employees";
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      userInfo: {
        username: "",
        password: ""
      }
    };
  },
  created() {
    this.loadDetailByid();
  },
  methods: {
    // 加载用户信息
    async loadDetailByid() {
      try {
        const res = await getUserDetailById(this.id);

        // 将用户信息填充到表单
        this.userInfo = res.data;
      } catch (error) {
        console.log("getUserDetailById", error);
      }
    },
    // 修改用户的账号信息
    async updateUserDetailById() {
      try {
        const res = await saveUserDetailById(this.userInfo);
        console.log("saveUserDetailById", res);

        this.$router.back();
      } catch (error) {
        console.log(error);
      }
    }
  }
};
</script>